from django.db import models
import time
import django.utils.timezone as timezone
import os
from djangoProject import settings
# Create your models here.


class team_model(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name="主序号")
    team_id = models.IntegerField(verbose_name="团队序号")
    user = models.CharField(max_length=20, verbose_name="队长账号名", default="账号名")
    team_name = models.CharField(max_length=30, default="团队名称", verbose_name="团队名称")
    accuracy = models.FloatField(verbose_name="模型精度")
    times = models.IntegerField(verbose_name="提交次数")
    track = models.IntegerField(choices=((0, "高职高专本科"), (1, "研究生")), verbose_name="赛道", default=0)
    team_num = models.IntegerField(verbose_name="团队人数", default=0)
    update_time = models.DateTimeField(verbose_name="提交时间", default=timezone.now)

    class Meta:
        db_table = 'team_model'
        verbose_name = "团队模型信息"
        verbose_name_plural = verbose_name


def teacher_directory_path(instance, filename):
    if os.path.exists(os.path.join(settings.MEDIA_ROOT, "last_team/{}".format(instance.uploader))):
        lists = os.path.join(settings.MEDIA_ROOT, "last_team/{}/".format(instance.uploader))
        fold_num = sum([os.path.isdir(lists + listx) for listx in os.listdir(lists)])
    else:
        fold_num = 0
    name = 'last_team/{}/{}/'.format(instance.uploader, fold_num + 1)
    return os.path.join(name, filename)


class TeamUpload(models.Model):
    """
    参赛团队上传模型
    """
    file = models.FileField(upload_to=teacher_directory_path, max_length=1024, verbose_name="比赛文件")
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")
    uploader = models.CharField(max_length=11, verbose_name="user")

    class Meta:
        db_table = "team_upload"


class TeamUploadLast(models.Model):
    """
    参赛团队上传模型
    """
    file = models.FileField(upload_to=teacher_directory_path, max_length=1024, verbose_name="比赛文件")
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name="上传时间")
    uploader = models.CharField(max_length=11, verbose_name="user")

    class Meta:
        db_table = "team_upload_last"


class team_modelLast(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name="主序号")
    team_id = models.IntegerField(verbose_name="团队序号")
    user = models.CharField(max_length=20, verbose_name="队长账号名", default="账号名")
    team_name = models.CharField(max_length=30, default="团队名称", verbose_name="团队名称")
    accuracy = models.FloatField(verbose_name="模型精度")
    times = models.IntegerField(verbose_name="提交次数")
    track = models.IntegerField(choices=((0, "高职高专本科"), (1, "研究生")), verbose_name="赛道", default=0)
    team_num = models.IntegerField(verbose_name="团队人数", default=0)
    update_time = models.DateTimeField(verbose_name="提交时间", default=timezone.now)

    class Meta:
        db_table = 'team_model_last'
        verbose_name = "团队决赛模型信息"
        verbose_name_plural = verbose_name